import request from '../../../api/request'
const TOPIC = {
  namespaced: true,
  state: {
    all: 0,
    myTopic: 0,
    allPage: {
      size: 20,
      num: 1,
      more: true,
      loadStatus: false
    },
    myPage: {
      size: 20,
      num: 1,
      more: true,
      loadStatus: false
    },
    allList: [],
    myTopicList: []
  },
  mutations: {
    updataAllList (state, data) {
      state.allList = state.allList.concat(data)
    },
    updataMyTopicList (state, data) {
      state.myTopicList = state.myTopicList.concat(data)
    },
    saveAllNum (state, data) {
      state.all = data
    },
    saveMyNum (state, data) {
      state.myTopic = data
    },
    updataAllStatus (state, data) {
      if (data.checked) {
        state.allList[data.index].checked = 0
        state.allList[data.index].count--
        state.myTopic--
        state.myTopicList.forEach((item, index) => {
          if (item.id === data.id) {
            console.log(state.myTopicList[index])
            state.myTopicList.splice(index, 1)
          }
        })
      } else {
        state.allList[data.index].checked = 1
        state.allList[data.index].count++
        state.myTopic++
        state.myTopicList.splice(0, 0, state.allList[data.index])
      }
    },
    updataMyStatus (state, data) {
      if (data.checked) {
        state.myTopicList[data.index].checked = 0
        state.myTopicList[data.index].count--
        state.myTopic--
        state.myTopicList.splice(data.index, 1)
      } else {
        state.myTopicList[data.index].checked = 1
        state.myTopicList[data.index].count++
      }
      state.allList.forEach((item) => {
        if (item.id === data.id) {
          if (data.checked) {
            item.checked = 0
            item.count--
          } else {
            item.checked = 1
            item.count++
          }
        }
      })
    },
    updataAllPageMsg (state, data) {
      state.allPage.num++
      state.allPage.more = data.next_page_url
    },
    updataMyPageMsg (state, data) {
      state.myPage.num++
      state.myPage.more = data.next_page_url
    },
    load (state, data) {
      if (data.type === 'all') {
        state.allPage.loadStatus = data.status
      } else {
        state.myPage.loadStatus = data.status
      }
    }
  },
  actions: {
    getAll (context) {
      context.commit('load', {type: 'all', status: true})
      request.post('getAllTopic', {page: context.state.allPage.num, pageSize: context.state.allPage.size}).then((response) => {
        if (response.data.code === '200') {
          context.commit('load', {type: 'all', status: false})
          context.commit('saveAllNum', response.data.data.total_count)
          context.commit('updataAllList', response.data.data.items)
          context.commit('updataAllPageMsg', response.data.data)
        }
      })
    },
    getMyTopic (context) {
      context.commit('load', {type: 'my', status: true})
      request.post('getSpecified', {page: context.state.myPage.num, pageSize: context.state.myPage.size}).then((response) => {
        if (response.data.code === '200') {
          context.commit('load', {type: 'my', status: false})
          context.commit('saveMyNum', response.data.data.total_count)
          context.commit('updataMyTopicList', response.data.data.items)
          context.commit('updataMyPageMsg', response.data.data)
        }
      })
    },
    subscribe (context, data) {
      let parameter = {
        do: data.checked,
        id: data.id
      }
      request.post('subManage', parameter).then((response) => {
        if (response.data.code === '200') {
          if (data.type === 'all') {
            context.commit('updataAllStatus', data)
          } else {
            context.commit('updataMyStatus', data)
          }
        }
      })
    }
  }
}
export default TOPIC
